package com.example.administrator.q1;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button button_login;
    Button button_register;

    ProgressBar progessBar;
    TextView progressBarTextView;

    /*
    *
    * TODO：
    *   将背景更改为更。。。
    *
    * */


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_say_hello);

        button_login = (Button) findViewById(R.id.button_login);
        button_register = (Button) findViewById(R.id.button_register);

        progessBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBarTextView = (TextView) findViewById(R.id.progressbarTextView);
        progressBarTextView.setVisibility(View.INVISIBLE);
        progessBar.setVisibility(View.INVISIBLE);
        progessBar.setMax(100);
        progessBar.setProgress(0);

        button_login.setOnClickListener(this);
        button_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_login:
                //
                new Thread() {
                    @Override
                    public void run() {
                        for (int i = 1; i <= 100; i++) {
                            try {
                                Message message = new Message();
                                message.what = 1;
                                message.getData().putInt("progress", i);
                                handler.sendMessage(message);
                                sleep(20);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }.start();
                progessBar.setVisibility(View.INVISIBLE);


                // finish();
                break;
            case R.id.button_register:
                //
                Toast.makeText(MainActivity.this, "你点错了！", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    int progress = msg.getData().getInt("progress");
                    progressBarTextView.setVisibility(View.VISIBLE);
                    progessBar.setVisibility(View.VISIBLE);
                    progessBar.setProgress(progress);
                    progressBarTextView.setText(progress + "%");
                    if (progress == 100) {
                        Intent intent = new Intent();
                        intent.setClass(MainActivity.this, sayYes.class);
                        startActivity(intent);
                        progessBar.setVisibility(View.INVISIBLE);
                    }
                    break;
            }
        }
    };


}
